<template>
    <div class="addComponentStyle" v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.8)">
        <div class="head">
            <div class="left">
                <i class="iconfont icon-zhuangxiu2"></i>
                <span>{{ nowType === "add" ? "新增" : "编辑" }}组件类型</span>
            </div>
            <div class="right">
                <el-button icon="el-icon-close" title="点击关闭" @click="closeThisWrapper"></el-button>
            </div>
        </div>
        <div class="body">
            <p class="title">自定义组件类型</p>
            <div class="row1">
                <div class="item1">
                    <span class="left required">类型名称：</span>
                    <el-input type="text" class="name" v-model="item.componentTypeName" maxlength="20" show-word-limit clearable></el-input>
                </div>
                <div class="item2">
                    <span class="left required">排序：</span>
                    <el-input type="number" class="sort" v-model="item.sort" maxlength="20" show-word-limit clearable></el-input>
                </div>
            </div>
            <!-- <div class="row2">
        <div class="item1">
          <span class="left required">选择自定义类型：</span>
          <el-select class="type" v-model="item.componentTypeId" clearable placeholder="类型">
            <el-option label="默认类型" value="1"></el-option>
          </el-select>
        </div>
      </div> 
      <div class="row3">
        <div class="item1">
          <span class="left required">上传自定义图标：</span>
          <div class="right">
            <el-upload
              class="upload-demo"
              action="https://jsonplaceholder.typicode.com/posts/"
              :on-change="elUploadOnChangeHandle"
              :limit="1"
              :multiple="false"
              accept="image/png,image/jpeg"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
          </div>
        </div>
      </div>-->
            <div class="row4">
                <el-button class="submit" type="primary" @click="submitHandle">提 交</el-button>
            </div>
        </div>
    </div>
</template>
<script>
import Bus from "@/utils/bus.js";
import headApi from '@/utils/hade';
export default {
    name: "addComponentStyle",
    data() {
        return {
            pageName:"新增组件类型",
            loading: false,
            nowType: "add",
            item: {
                componentTypeName: "",
                id: null,
                sort: 0,
            },
        };
    },
    created() {},
    props: [],
    components: {},
    mounted() {
        //console.log(this);
    },
    methods: {
        //选择组件类型
        elUploadOnChangeHandle(e) {
            //console.log(e);
        },
        //关闭当前模块
        closeThisWrapper() {
            Bus.$emit("editComponentStyle", { show: false });
        },
        //设为默认
        setDefault() {
            this.item = {
                componentTypeName: "",
                id: null,
                sort: 0,
            };
        },
        //提交保存
        submitHandle() {
            if (!this.item.componentTypeName) {
                this.$message.warning("请输入组件类型名称");
                return false;
            }
            if (this.item.id) {
                this.saveEdit();
            } else {
                this.saveCreate();
            }
        },
        //保存新增
        saveCreate() {
            if (getToken()) {
                this.loading = true;
                this.axios
                    .post(
                        `${headApi}/appComponentType`,
                        {
                            componentTypeName: this.item.componentTypeName,
                            sort: this.item.sort,
                        },
                        {
                            headers: { Authorization: "Bearer " + getToken() },
                        }
                    )
                    .then((res) => {
                        this.loading = false;
                        if (res && res.data) {
                            let { code, data, msg } = res.data;
                            if (code === 200) {
                                this.$message.success("提交成功");
                                this.$parent.$refs.componentStyle.init();
                                setTimeout(() => {
                                    this.closeThisWrapper();
                                    this.setDefault();
                                }, 1500);
                            } else {
                                this.$message.warning(msg);
                            }
                        } else {
                            this.$message.warning("接口请求失败");
                        }
                    })
                    .catch((err) => {
                        //console.log(err);
                        this.loading = false;
                    });
            }
        },
        //保存编辑
        saveEdit() {
            this.loading = true;
            this.axios
                .put(`${headApi}/appComponentType`, {
                    componentTypeName: this.item.componentTypeName,
                    id: this.item.id,
                    sort: this.item.sort,
                })
                .then((res) => {
                    this.loading = false;
                    if (res && res.data) {
                        let { code, data, msg } = res.data;
                        if (code === 200) {
                            this.$message.success("提交成功");
                            this.$parent.$refs.componentStyle.init();
                            setTimeout(() => {
                                this.closeThisWrapper();
                                this.setDefault();
                            }, 1500);
                        } else {
                            this.$message.warning(msg);
                        }
                    } else {
                        this.$message.warning("接口请求失败");
                    }
                })
                .catch((err) => {
                    //console.log(err);
                    this.loading = false;
                });
        },
    },
    computed: {},
    watch: {},
};
</script>
<style scoped lang="scss">
.addComponentStyle {
    position: absolute;
    left: 0;
    box-sizing: border-box;
    top: 0;
    width: 100%;
    padding: 20px;
    height: 100%;
    z-index: 10;
    background-color: #fff;
    & > .head {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-bottom: 20px;
        height: 48px;
        border-bottom: 1px solid #eee;
        margin-bottom: 20px;
        .left {
            i {
                color: #1890ff;
                margin-right: 5px;
                font-size: 15px;
            }
            span {
                font-size: 16px;
            }
        }
        .right {
            .el-button {
                border: none;
            }
            .el-button::after {
                border: none;
            }
        }
    }
    & > .body {
        height: calc(100% - 50px);
        display: flex;
        flex-direction: column;
        .title {
            width: 100%;
            height: 30px;
            display: flex;
            margin-bottom: 50px;
            justify-content: center;
            align-items: center;
        }
        .row1 {
            width: 100%;
            height: 50px;
            display: flex;
            margin-bottom: 50px;
            align-items: center;
            .item1 {
                width: 50%;
                display: flex;
                height: 50px;
                align-items: center;
                justify-content: flex-end;
                .left {
                    position: relative;
                    font-size: 14px;
                    &.required::before {
                        content: "*";
                        position: absolute;
                        left: -10px;
                        top: 4px;
                        color: red;
                    }
                }
                .name {
                    width: 240px;
                    margin-right: 30px;
                }
            }
            .item2 {
                width: 50%;
                display: flex;
                height: 50px;
                align-items: center;
                justify-content: flex-start;
                .left {
                    position: relative;
                    margin-left: 30px;
                    font-size: 14px;
                    &.required::before {
                        content: "*";
                        position: absolute;
                        left: -10px;
                        top: 4px;
                        color: red;
                    }
                }
                .sort {
                    width: 240px;
                }
            }
        }
        .row2 {
            width: 100%;
            height: 50px;
            margin-bottom: 50px;
            display: flex;
            align-items: center;
            .item1 {
                width: 50%;
                display: flex;
                height: 50px;
                align-items: center;
                justify-content: flex-end;
                .left {
                    position: relative;
                    font-size: 14px;
                    &.required::before {
                        content: "*";
                        position: absolute;
                        left: -10px;
                        top: 4px;
                        color: red;
                    }
                }
                .type {
                    width: 240px;
                    margin-right: 30px;
                }
            }
        }
        .row3 {
            width: 100%;
            height: 50px;
            display: flex;
            margin-bottom: 100px;
            align-items: center;
            .item1 {
                width: 50%;
                display: flex;
                height: 50px;
                justify-content: flex-end;
                .left {
                    position: relative;
                    display: flex;
                    align-items: center;
                    font-size: 14px;
                    height: 32px;
                    &.required::before {
                        content: "*";
                        position: absolute;
                        left: -10px;
                        top: 4px;
                        color: red;
                    }
                }
                .right {
                    width: 240px;
                    margin-right: 30px;
                }
            }
        }
        .row4 {
            width: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
            .submit {
                width: 130px;
            }
        }
    }
}
</style>
<style lang="scss">
.addCustomModule {
    .name.el-input {
        input {
            padding-right: 70px;
        }
    }
}
</style>
